import { _decorator, Component, Node, tween, Vec3 } from 'cc';
import { App } from '../App';
import { EventConst, Singleton } from '../common/Common';
import UtilTool from '../tools/UtilTool';
const { ccclass, property } = _decorator;

// 个人信息
class PlayerData extends Singleton<PlayerData>{
    public user = {
        nickname : "",	        // 昵称
        headUrl : "",		    // 头像
        headId : 0,		        // 头像ID
        addressUrl : "myAdddress",		// 钱包地址
        coin : 0,		        // 金币
        coin_str : "0",         // 金币字符串
        starCoin : 0,		    // 星币
        starCoin_str : "0",     // 星币字符串

        usdt : "USDT",
        chainIndex : 0,          // 选择的链索引
        newPlayer : true,        // 是否是新玩家
        token : 0,               // 登录的token

        defaultCue : 0,          // 默认球杆编号
        currentCue : 6,          // 正在使用的球杆

        matchMinGold : 0,        // 最小匹配多人房间的入场金额
    }
    
    // 登录返回的用户数据
    playerInfo = {
        "id": 0,
        "userId": 0,
        "ustd": 0,
        "isNew":true,
        "isHolder":false, // 是否是球室持有人
        "currentUseClub":0, // 当前使用的球杆
    }

    // 登录返回的配置数据
    gameConfig = {
        "chargeScale": 0,   // 购买比例(1U对应的金币数)
        "userPeriodMaxLimit": 0,    // 当期用户最大下单限制
        "userCurrentOrderMaxLimit": 0,  // 单注用户最大下单限制
        "userCurrentOrderMinLimit": 0,  // 单注用户最小下单限制

        "withdrawMinLimit": 0,  // 提现最小限制
        "chargeMinLimit": 0,    // 充值最小限制
 
        "buyTime": 0,       // 下注时间（单位：秒）
        "countDownTime": 0, // 倒计时时间（单位：秒）
        "hitTime": 0,       // 击球时间（单位: 秒）
        "baseOdds": 0,       // 基础赔率(幸运台球)
        "hitOdds": 0,       // 击中赔率(幸运台球)
    }

    // 血流模式的配置数据
    bloodConfig = {
        "whiteBallInHoleDeductScore": 0,    // 血流模式白球进洞扣除分数
        
        "masterMinLimit": 0,                // 血流模式高级场最小入场金额
        "masterBaseScore": 0,               // 血流模式高级场基础分数
        "apprenticeMinLimit":0,             // 血流模式初级场最小入场金额
        "apprenticeBaseScore":0,            // 血流模式初级场基础分数
    }

    // 默认球杆信息
    defaultCueInfo = {
        "charm": 1,         // 魅力值
        "number": 10,       // 编号
        "sightLine": 3,     // 瞄准线
        "vigor": 100        // 力度大
    }

    // 我的球杆信息
    mineCueInfo = {
        "charm": 1,         // 魅力值
        "number": 10,       // 编号
        "sightLine": 3,     // 瞄准线
        "vigor": 100        // 力度大
    }


    public init () {
        
    }

    /** 
    * 设置金币
    */
    public setCoin(count, add=false) {
        if (count==null) {
            return
        }
        if (add) {
            this.user.coin = this.user.coin + count
        }
        else{
            this.user.coin = count
        }

        if (this.user.coin<0) {
            this.user.coin = 0
        }

        this.user.coin_str = UtilTool.formatValue(this.user.coin,{digit:1,type:2})
        App.Event.emit(EventConst.UPDATE_COIN, this.user.coin_str)
    }

    /** 
    * 设置星币
    */
    public setStarCoin(count, add=false) {
        if (count==null) {
            return
        }
        if (add) {
            this.user.starCoin = this.user.starCoin + count
        }
        else{
            this.user.starCoin = count
        }

        if (this.user.starCoin<0) {
            this.user.starCoin = 0
        }

        this.user.starCoin_str = UtilTool.formatValue(this.user.starCoin,{digit:1,type:2})
        App.Event.emit(EventConst.UPDATE_STARCOIN, this.user.starCoin_str)
    }

    /** 
    * 设置昵称
    */
    public setNick(name) {
        if (name==null) return
        
        this.user.nickname = name 
        App.Event.emit(EventConst.UPDATE_SET_NICK)
    }

    /**
     * 设置球室持有人
     */
    public setHolder(isHolder) {
        this.playerInfo.isHolder = isHolder
    }

    /** 
    * 设置用户信息
    */
    public setplayerInfo(data) {
        if (data==null) {
            return
        }

        this.gameConfig.chargeScale = data.chargeScale
        this.gameConfig.userPeriodMaxLimit = data.userPeriodMaxLimit
        this.gameConfig.userCurrentOrderMaxLimit = data.userCurrentOrderMaxLimit
        this.gameConfig.userCurrentOrderMinLimit = data.userCurrentOrderMinLimit
        this.gameConfig.withdrawMinLimit = data.withdrawMinLimit
        this.gameConfig.chargeMinLimit = data.chargeMinLimit
        this.gameConfig.buyTime = data.buyTime || 180
        this.gameConfig.countDownTime = data.countDownTime || 30
        this.gameConfig.hitTime = data.hitTime || 90
        this.gameConfig.baseOdds = data.baseOdds
        this.gameConfig.hitOdds = data.hitOdds

        // 血流模式配置数据
        this.bloodConfig.whiteBallInHoleDeductScore = data.whiteBallInHoleDeductScore
        this.bloodConfig.masterMinLimit = data.masterMinLimit
        this.bloodConfig.masterBaseScore = data.masterBaseScore
        this.bloodConfig.apprenticeMinLimit = data.apprenticeMinLimit
        this.bloodConfig.apprenticeBaseScore = data.apprenticeBaseScore

        this.playerInfo.id = data.player.id
        this.playerInfo.userId = data.player.userId
        this.playerInfo.ustd = data.player.ustd
        this.playerInfo.isNew = data.player.isNew
        this.playerInfo.isHolder = data.player.isHolder === 1
        this.playerInfo.currentUseClub = data.player.currentUseClub || 0

        
        this.user.newPlayer = data.player.isNew==1
        this.user.matchMinGold = data.matchMinGold || 0
        this.setCoin(data.player.ustd)
        this.setStarCoin(data.player.experienceNum)
        this.user.currentCue = this.playerInfo.currentUseClub
        this.user.headId = data.player.headId
        this.user.nickname = data.player.nickname

        // 我的球杆信息 
        this.mineCueInfo.charm = data.player.currentUserClubInfo.charm
        this.mineCueInfo.number = data.player.currentUserClubInfo.number
        this.mineCueInfo.sightLine = data.player.currentUserClubInfo.sightLine
        this.mineCueInfo.vigor = data.player.currentUserClubInfo.vigor
    }
}

export const PD: PlayerData = PlayerData.getInstance<PlayerData>(PlayerData);

